package br.com.nooclix.salon.controller;

import br.com.nooclix.salon.application.CustomerApplication;
import br.com.nooclix.salon.controller.util.ResponseData;
import br.com.nooclix.salon.domain.customer.Customer;
import br.com.nooclix.salon.infrastructure.util.ApplicationContextRegister;

public class CustomerController {

	private CustomerApplication customerApplication = ApplicationContextRegister
			.getBean("customerApplication", CustomerApplication.class);

	public ResponseData persist(Customer customer) {
		ResponseData responseData = new ResponseData();

		try {

			// configurando para ser NULL por causa do FLEX ter padrao 0(ZERO)
			customer.setCode(null);
			customer.getPerson().setCode(null);
			customer.getPerson().getAddress().setCode(null);

			customerApplication.persist(customer);
			responseData.setStatus(ResponseData.OK);
		} catch (Exception e) {
			e.printStackTrace();
			responseData.setMessage(e.getMessage());
			responseData.setStatus(ResponseData.FAILED);
		}

		return responseData;

	}

	public ResponseData update(Customer customer) {
		ResponseData responseData = new ResponseData();

		try {
			customerApplication.update(customer);
			responseData.setStatus(ResponseData.OK);
		} catch (Exception e) {
			responseData.setMessage(e.getMessage());
			responseData.setStatus(ResponseData.FAILED);
		}

		return responseData;

	}

	public ResponseData remove(Customer customer) {
		ResponseData responseData = new ResponseData();

		try {
			customerApplication.delete(customer);
			responseData.setStatus(ResponseData.OK);
		} catch (Exception e) {
			responseData.setMessage(e.getMessage());
			responseData.setStatus(ResponseData.FAILED);
		}

		return responseData;

	}

	public ResponseData findByName(String name) {
		ResponseData responseData = new ResponseData();

		try {
			responseData.setItems(customerApplication.findByName(name));
			responseData.setStatus(ResponseData.OK);
		} catch (Exception e) {
			responseData.setMessage(e.getMessage());
			responseData.setStatus(ResponseData.FAILED);
		}

		return responseData;

	}

}